package com.ysstech.dataway.controller;


import com.alibaba.fastjson.JSONArray;
import com.ysstech.common.annotation.YssLog;
import com.ysstech.common.annotation.YssPermissions;
import com.ysstech.common.controller.BaseController;
import com.ysstech.common.entity.TreeNode;
import com.ysstech.common.exception.BusinessException;
import com.ysstech.common.util.resultutil.RestResult;
import com.ysstech.common.util.resultutil.ResultGenerator;
import com.ysstech.dataway.entity.Dataway;
import com.ysstech.dataway.feign.FeignFinanceService;
import com.ysstech.dataway.thread.DatawayMainGenerator;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 数据处理 前端控制器
 * </p>
 *
 * @author lishuangliang
 * @since 2022-01-18
 */
@Controller
@Slf4j
@Api(tags = "交易数据管理api")
@RequestMapping("/dataway")
public class DatawayController extends BaseController {
    @Autowired
    private DatawayMainGenerator datawayMainGenerator;
    @Autowired
    private FeignFinanceService feignFinanceService;

    @YssPermissions(value = {"dataway:deal:upload"})
    @ApiOperation(value = "跳转估值表管理页面")
    @YssLog(module = "交易数据管理", remark = "跳转数据管理页面")
    @RequestMapping(value = "/page")
    public String page() throws Exception {
        return "dataway/dataway";
    }

    @YssPermissions(value = {"dataway:deal:upload"})
    @ApiOperation(value = "获取交易数据上传表页面初始值")
    @YssLog(module = "交易数据管理", remark = "获取交易数据上传页面初始值")
    @PostMapping(value = "/init")
    @ResponseBody
    public RestResult init() throws Exception {
        Map<String, Object> map = new HashMap<>();
        List<TreeNode> lsetlists = feignFinanceService.queryAccountTreeByType(redisCacheManager.getLoginUserInfo().getOrgId(), null, null);
        map.put("lsetlists", lsetlists);
        return ResultGenerator.getSuccessResult("获取页面初始化信息成功", map);
    }


    @YssPermissions(value = {"dataway:deal:upload"})
    @ApiOperation(value = "上传交易数据")
    @YssLog(module = "交易数据管理", remark = "上传交易数据")
    @PostMapping(value = "/upload")
    @ResponseBody
    public RestResult upload(Dataway dataway, MultipartFile file) throws Exception {
        if (StringUtils.isBlank(dataway.getAccountId())) {
            throw new BusinessException("获取基金信息失败，请稍后重试！");
        }
        List<String> list = JSONArray.parseArray(dataway.getAccountId(), String.class);
        datawayMainGenerator.renewValuation(list, dataway);
        return ResultGenerator.getSuccessResult("上传交易数据成功！");
    }
}
